package la.iok.hzsvn.lewin.movie.controller;

import la.iok.hzsvn.lewin.movie.core.exception.ErrorCode;
import la.iok.hzsvn.lewin.movie.core.model.MovieVo;
import la.iok.hzsvn.lewin.movie.entity.MovieInstance;
import la.iok.hzsvn.lewin.movie.service.MovieInstanceService;
import la.iok.hzsvn.share.response.ListDataResponse;
import la.iok.hzsvn.share.response.OperatorResponse;
import la.iok.hzsvn.share.response.OperatorResponses;
import la.iok.hzsvn.share.response.SingleDataResponse;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("internal-api/movieInstances")
public class MovieInstanceInternalController {
    private final MovieInstanceService movieInstanceService;

    public MovieInstanceInternalController(MovieInstanceService movieInstanceService) {
        this.movieInstanceService = movieInstanceService;
    }

    @GetMapping("movie/{movieId}/{version}")
    public SingleDataResponse<MovieVo> movieInfo(@PathVariable Long movieId, @PathVariable String version){
        MovieInstance mi = movieInstanceService.movieInst(movieId,version);
        ErrorCode.MOVIE_NOT_EXIST.assertNotNull(mi,movieId);
        return new SingleDataResponse<>(movieInstanceService.po2vo(mi));
    }
    @PutMapping("movie/{movieId}/{version}/playTimes")
    public OperatorResponse incrementPlayTimes(@PathVariable Long movieId, @PathVariable String version){
        movieInstanceService.incrementPlayTimes(movieId,version);
        return OperatorResponses.success();
    }

    @GetMapping("latest")
    public ListDataResponse<MovieVo> latestMovies(Long[] movieIds) {
        return new ListDataResponse<>(movieInstanceService.latestMovies(movieIds));
    }
}
